package com.example.admin.controller.web.activiti;

import com.example.common.core.response.AjaxResult;
import com.example.common.util.security.SecurityUtil;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.activiti.engine.repository.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 陈建峰
 * @date 2022/11/1 15:14
 * @Description 工作流
 */
@RestController
@RequestMapping("/activiti")
public class ActivitiController {


    /**
     * 初始化部署模型
     *
     * @return
     */
    @PostMapping("/deployment")
    public AjaxResult deploymentModel(String fileName, String modelName) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("processes/" + fileName + ".bpmn20.xml")
                .addClasspathResource("processes/" + fileName + ".png").name(modelName).deploy();
        return AjaxResult.success(deploy);
    }


    /**
     * 查询模型列表
     *
     * @return
     */
    @GetMapping("/getModelList")
    public AjaxResult getModelList() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //创建查询部署的对象，可以理解为查询 act_re_deployment
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();

        List<ProcessDefinition> list = processDefinitionQuery.list();
        ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();
        HashMap<String, Object>  stringObjectHashMap= null;
        for (ProcessDefinition processDefinition : list) {
            stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("id",processDefinition.getId());
            stringObjectHashMap.put("name",processDefinition.getName());
            stringObjectHashMap.put("version",processDefinition.getVersion());
            stringObjectHashMap.put("act_re_deployment_id",processDefinition.getDeploymentId());
            hashMaps.add(stringObjectHashMap);
        }
        return AjaxResult.success(hashMaps);


    }

    /**
     * 启动部署的模型
     *
     * @return
     */
    @PostMapping("/startModel")
    public AjaxResult startModel(String instanceId) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("peopleId", SecurityUtil.getLoginUser().getId());
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(instanceId, variables);
        return AjaxResult.success(processInstance.getId());
    }


    /**
     * 完成某个任务
     */
    @PostMapping("/nextStep")
    public void nextStep(String taskId) {
        //获取引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取TaskService
        TaskService taskService = processEngine.getTaskService();
        //TaskQuery taskQuery = taskService.createTaskQuery().taskAssignee(assignee);
        taskService.complete(taskId);
    }


    /**
     * 查看自己任务下是否有任务可执行
     */
    @PostMapping("/taskList")
    public AjaxResult getTaskList() {
        //获取引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取TaskService
        TaskService taskService = processEngine.getTaskService();
        //TaskQuery taskQuery = taskService.createTaskQuery().taskAssignee(assignee);
        List<Task> list = taskService.createTaskQuery().taskAssignee(SecurityUtil.getLoginUser().getId().toString()).list();

        List<HashMap<String, Object>> hashMaps = new ArrayList<>();
        HashMap<String, Object> stringObjectHashMap =null;
        for (Task task : list) {
            stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("id",task.getId());
            stringObjectHashMap.put("userId",task.getAssignee());
            stringObjectHashMap.put("name",task.getName());
            hashMaps.add(stringObjectHashMap);
        }

        return AjaxResult.success(hashMaps);
    }

    /**
     * 查询执行完毕的任务
     */
    @PostMapping("/over/taskList")
    public AjaxResult getTaskOverList() {
        //获取引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        HistoryService historyService = processEngine.getHistoryService();
        HistoricTaskInstanceQuery historicTaskInstanceQuery = historyService.createHistoricTaskInstanceQuery();
        //查询执行完毕的任务
        List<HistoricTaskInstance> list = historicTaskInstanceQuery.finished().list();
        return AjaxResult.success(list);

    }

    /**
     * 查询属于当前用户的任务
     */
    @GetMapping("/getUserTaskList")
    public AjaxResult getUserTaskList() {
        //获取引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        HistoryService historyService = processEngine.getHistoryService();
        //  创建历史任务实例对象
        HistoricTaskInstanceQuery historicTaskInstanceQuery = historyService.createHistoricTaskInstanceQuery();
        List<HistoricTaskInstance> list = historicTaskInstanceQuery.taskAssignee(SecurityUtil.getLoginUser().getId().toString()).list();
        return AjaxResult.success(list);
    }


    /**
     * 查询属于当前用户的历史任务
     */
    @GetMapping("/getUserTaskHisList")
    public AjaxResult getUserTaskHisList() {
        //获取引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        HistoryService historyService = processEngine.getHistoryService();
        //  创建历史任务实例对象
        HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
        List<HistoricActivityInstance> list = historicActivityInstanceQuery.taskAssignee(SecurityUtil.getLoginUser().getId().toString()).list();
        return AjaxResult.success(list);
    }



//a89dd7d1-59b6-11ed-adbb-e8d8d14603ba
}
//https://blog.csdn.net/python113/article/details/124059488